%
% Table 1 on Posterior Estimates
%
% Table 1 in Appendix
%

clear
clc

%% Estimates

load mhall_01-Apr-2021.mat

SET.EST.burn_in = 1/2;

params_x_ = [] ;
 
for runs=2:maxproc
    params_x_ = [params_x_ ; params_x(round(length(params_x)*SET.EST.burn_in):end,:,runs)] ; %#ok<AGROW>
end

for ii=1:length(params_x_(1,:))
    %[N,X] = hist(params_x_(:,ii),5000) ;
    [a,b]=ksdensity(params_x_(:,ii),'NumPoints',200);
    %tmp = X(N==max(N)) ;
    post_kern_xx_mode(ii) = b(a==max(a)) ;
end

p = zeros(size(post_kern_xx_mode,1),1);
x=gamrnd(3,1/1,1000000,1) ; x=1./x; 

prior_table = [ ...
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ] ;

tableout = [] ;

tableout = [tableout post_kern_xx_mode' prctile(params_x_,[50 5 95])'] ;

tableout_for_appendix = [] ;
tableout_for_appendix = [prior_table post_kern_xx_mode' prctile(params_x_,[5 95])'] ;

disp('Priors, With Demographics') ;
disp(''); 
latex(prior_table, '%.1f', 'nomath' ) ;

disp(''); 
disp('Posteriors, With Demographics') ;
disp(''); 
latex(tableout, '%.3f', 'nomath' ) ;

%% Estimates Without Demographics

load mhall_01-Apr-2021_nodemographics.mat

SET.EST.burn_in = 1/2;

params_x_ = [] ;
 
for runs=2:maxproc
    params_x_ = [params_x_ ; params_x(round(length(params_x)*SET.EST.burn_in):end,:,runs)] ; %#ok<AGROW>
end

for ii=1:length(params_x_(1,:))
    %[N,X] = hist(params_x_(:,ii),5000) ;
    [a,b]=ksdensity(params_x_(:,ii),'NumPoints',200);
    %tmp = X(N==max(N)) ;
    post_kern_xx_mode(ii) = b(a==max(a)) ;
end

p = zeros(size(post_kern_xx_mode,1),1);
x=gamrnd(3,1/1,1000000,1) ; x=1./x; 

prior_table = [ ...
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[betastat(5.05556,5.05556) betainv([0.05 0.95],5.05556,5.05556)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ; 
[unifstat(eps,4) unifinv([0.05 0.95],eps,4)] ] ;

tableout = [] ;

tableout = [tableout post_kern_xx_mode' prctile(params_x_,[50 5 95])'] ;

tableout_for_appendix = [tableout_for_appendix post_kern_xx_mode' prctile(params_x_,[5 95])'] ;

disp('Priors, Without Demographics') ;
disp(''); 
latex(prior_table, '%.1f', 'nomath' ) ;

disp(''); 
disp('Posteriors, Without Demographics') ;
disp(''); 
latex(tableout, '%.3f', 'nomath' ) ;

disp(''); 
disp('Posteriors, Appendix Table, Prior/With Demo/Without Demo') ;
disp(''); 
latex(tableout_for_appendix, '%.2f', 'nomath' ) ;
